package com.szlanyou.tcp_server_demo.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.camnter.easyrecyclerview.adapter.EasyRecyclerViewAdapter;
import com.camnter.easyrecyclerview.holder.EasyRecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表Adapter基类
 * Created by Zhang on 2015/12/24.
 *
 * @author 陈章
 */
public abstract class BaseEasyRecyclerViewAdapter<T> extends EasyRecyclerViewAdapter {
    private static final String TAG = "BaseEasyRecycler";
    protected List<T> mData;
    protected EasyRecyclerViewHolder.OnItemClickListener onItemClickListener;
    protected Context mContext;

    public BaseEasyRecyclerViewAdapter(List<T> mData) {
        this.mData = mData;
    }

    public BaseEasyRecyclerViewAdapter() {
        this.mData = new ArrayList<>();
    }

    public BaseEasyRecyclerViewAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int[] getItemLayouts() {
        return new int[]{getLayoutId()};
    }

    @Override
    public void onBindRecycleViewHolder(EasyRecyclerViewHolder viewHolder, int position) {
        bindData(mData.get(position), position, viewHolder);
        if (onItemClickListener != null) {
            viewHolder.setOnItemClickListener(onItemClickListener, position);
        }
    }


    @Override
    public int getRecycleViewItemType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    protected abstract int getLayoutId();

    protected abstract void bindData(T data, int position, EasyRecyclerViewHolder viewHolder);

    public void set(List<T> list, boolean autoNotify) {
        if (list == null || list.size() == 0) {
            return;
        }
        this.mData = list;
        if (autoNotify) {
            notifyDataSetChangedOnUiThread();
        }
    }


    public void set(List<T> list) {
        set(list, true);
    }

    @Override
    public void clear() {
        if (this.mData != null) {
            this.mData.clear();
        }
        notifyDataSetChangedOnUiThread();
    }


    @Override
    public void remove(Object o) {
        this.mData.remove(o);
        notifyDataSetChangedOnUiThread();
    }

    public List<T> getData() {
        return mData == null ? new ArrayList<T>() : mData;
    }

    public void addItem(T obj) {
        if (mData != null) {
            mData.add(obj);
        }
        notifyDataSetChangedOnUiThread();
    }

    public void addAll(List<T> list) {
        this.addAll(list, true);
    }

    public void addAll(List<T> list, boolean autoNotify) {           //防止在子线程里刷新
        if (list == null) {
            return;
        }
        if (this.mData == null) {
            this.mData = new ArrayList<T>();
        }
        this.mData.addAll(list);
        if (autoNotify) {
            notifyDataSetChangedOnUiThread();
        }
    }

    public int getDataSize() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    /**
     * * set the on item click listener
     * 设置Item的点击事件
     */
    public void bindItemClickListener(final EasyRecyclerViewHolder.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    protected void skip(String key, String value, Class<?> clazz, boolean isCloseSelf) {
        Intent intent = new Intent();
        intent.setClass(mContext, clazz);
        if (!TextUtils.isEmpty(key)) {
            intent.putExtra(key, value);
        }
        mContext.startActivity(intent);
        if (isCloseSelf) {
            ((Activity) mContext).finish();
        }
    }

    /**
     * 用getDataSize
     */
    @Override
    @Deprecated
    public int getListSize() {
        return super.getListSize();
    }

    /**
     * 用set()
     */
    @Override
    @Deprecated
    public void setList(List list) {
        super.setList(list);
    }

    /**
     * 用getData()
     */
    @Override
    @Deprecated
    public List getList() {
        return super.getList();
    }

    public void notifyDataSetChangedOnUiThread(){
        MainHandler.getInstance().post(new Runnable() {
            @Override
            public void run() {
                BaseEasyRecyclerViewAdapter.this.notifyDataSetChanged();
            }
        });
    }

    public void notifyItemChangedOnUiThread(final int position){
        MainHandler.getInstance().post(new Runnable() {
            @Override
            public void run() {
                BaseEasyRecyclerViewAdapter.this.notifyItemChanged(position);
            }
        });
    }

}
